<?php

namespace app\common\command;

use app\admin\controller\MemberMarketing;
use app\common\tools\Schedule;
use app\shop\model\ShopSeckillActivity;
use app\shop\service\Order;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use tp5er\Backup;

/**
 * 定时任务
 * Class Crontab
 * @package app\common\command
 */
class Crontab extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('crontab')->setDescription('Used for scheduling timed tasks');
    }
    protected function execute(Input $input, Output $output)
    {
        $shopModulePath = app()->getAppPath().'shop';
        $schedule = new Schedule;
        $schedule->call(function () use($shopModulePath) {
            if(is_dir($shopModulePath)){
                //会员自动过期
                Order::card();
                //删除订单
                Order::del();
            }

        })->everyMinute();
        $schedule->call(function ()use($shopModulePath) {
            if(is_dir($shopModulePath)) {
                //自动确认收货
                Order::confirm();
            }
            //数据库备份
            $this->dataBackUp();
        })->everyDay(sysconf('database_day'));

        //营销报表统计
        $schedule->call(function () {
            $memberMarketing = new MemberMarketing();
            $memberMarketing->memberMarketing();
        })->everyMinute(1);


        $output->writeln("<info>crontab success</info>");
    }
    /**
     * 数据库备份
     * @Author: rocky
     * 2019/9/27 14:48
     */
    private function dataBackUp()
    {
        $backupsConfig = [
            'path'=>app()->getRootPath().'backup/',//数据库备份路径
        ];

        $db = new Backup($backupsConfig);
        $sv = $db->fileList();
        $leng = count($sv);
        $on = sysconf('database_is_auto');
        $day = sysconf('database_day');
        $max_number = sysconf('database_number');
        if ($on && $day) {
            $time = time();
            $file = ['name' => date('Ymd-His', $time), 'part' => 1];
            //获取数据表列表
            set_time_limit(0);
            $tableList = $db->dataList();
            $is_del = false;
            foreach ($tableList as $k => $v) {
                $start = $db->setFile($file)->backup($v['name'], 0);
                if ($start == 0) {
                    continue;
                } else {
                    $is_del = true;
                    break;
                }
            }
            if ($is_del) {
                //备份失败
                $db->delFile($time);
                sysoplog('备份数据库', '备份失败!请稍后再试');
            } else {
                $delCount = $leng + 1 - $max_number;
                for ($i = 0; $i < $delCount; $i++) {
                    $backup = array_shift($sv);
                    $db->delFile($backup['time']);
                }
                sysoplog('备份数据库', '备份成功');
            }
        }
    }
}
